﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersistentObject.Common.Schema
{
    public class Table
    {
        /// <summary>
        /// Initialize an instance of table info
        /// </summary>
        public Table()
        {
            NonPersist = false;
        }

        public Table(TableInfo info, int sequence)
        {
            TableName = info.Name;
            Type = info.Type;
            ClassName = Type.FullName;
            NonPersist = info.NonPersist;
            Sequence = sequence;
            ColumnList = new List<Column>();
        }

        /// <summary>
        /// Indicate the persist table name
        /// </summary>
        /// 
        public string TableName { get; internal set; }

        public string ClassName { get; internal set; }

        /// <summary>
        /// Indicate the mapping clr type
        /// </summary>
        public Type Type { get; internal set; }

        /// <summary>
        /// Indicate whether the type would be persisted
        /// </summary>
        public bool NonPersist { get; internal set; }

        /// <summary>
        /// Contain the list of columns
        /// </summary>
        public List<Column> ColumnList { get; internal set; }

        public Column KeyColumn { get; internal set; }

        public int Sequence { get; internal set; }

        public Column ForeignColumn { get; internal set; }

        public bool IsInnerExists { get; internal set; }

        public override string ToString()
        {
            return string.Format("{0}, key:{1}, seq:{2}", TableName, KeyColumn.ColumnName, Sequence);
        }

        public Column GetMappingColumn(string propertyName)
        {
            Table table = this;
            string[] sections = propertyName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < sections.Length - 1; i++)
            {
                bool hasFoundForeign = false;
                foreach (var foreignColumn in table.ColumnList.FindAll((item) => item.IsForeignKeyColumn))
                {
                    if (foreignColumn.PropertyName == sections[i])
                    {
                        table = foreignColumn.ForeignTable;
                        hasFoundForeign = true;
                    }
                }
                if (!hasFoundForeign)
                {
                    return null;
                }
            }

            string lastPropertyName = sections[sections.Length - 1];

            if (table.KeyColumn.PropertyName == lastPropertyName)
            {
                return table.KeyColumn;
            }
            else
            {
                foreach (var column in table.ColumnList)
                {
                    if (column.PropertyName == lastPropertyName)
                    {
                        return column;
                    }
                }
            }

            return null;
        }
    }
}
